package com.msb.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

public class GamePanel extends JPanel {
    int lengh;
    String direction;
    Timer timer;//定时器
    int foodX;
    int foodY;
    int score;
    boolean isDie=false;
    int [] snakeX=new int [100];
    int [] snakeY=new int [100];
    //是否开始
    boolean isStart=false;

    public void init() {
        lengh =3 ;
        snakeX[0] = 175;
        snakeY[0] = 275;
        snakeX[1] = 150;
        snakeY[1] = 275;
        snakeX[2] = 125;
        snakeY[2] = 275;
        direction="R";
        foodX=300;
        foodY=200;
    }
   public  GamePanel (){
         init();
         //焦点。面板
       this.setFocusable(true);
       this.addKeyListener(new KeyAdapter() {
           @Override
           public void keyPressed(KeyEvent e) {
               super.keyPressed(e);
               int keyCode = e.getKeyCode();
               System.out.println(keyCode);
               if(keyCode==KeyEvent.VK_SPACE){
                   if (isDie){
                       init();
                       isDie=false;
                   }else {
                       isStart=!isStart;
                       repaint();
                   }


               }
               //监听按键换蛇头图
               if(keyCode==KeyEvent.VK_UP & !direction.equals("D")){
                   direction="U";
               }
               if(keyCode==KeyEvent.VK_DOWN & !direction.equals("U")){
                   direction="D";
               }
               if(keyCode==KeyEvent.VK_LEFT & !direction.equals("R")){
                   direction="L";
               }
               if(keyCode==KeyEvent.VK_RIGHT & !direction.equals("L")){
                    direction="R";
               }

           }
       });
       timer=new Timer(100,new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(isStart && isDie==false){
                   for (int i = lengh-1; i >0 ; i--) {
                        snakeX[i] = snakeX[i-1];
                        snakeY[i] = snakeY[i-1];//身子前进一个格
                   }

                   if("R".equals(direction)){
                       snakeX[0] +=25;//移动头
                   }
                   if("L".equals(direction)){
                       snakeX[0] -=25;
                   }
                   if("U".equals(direction)){
                       snakeY[0] -=25;
                   }
                   if("D".equals(direction)){
                       snakeY[0] +=25;
                   }

                   //防止超边界
                   if(snakeX[0]>750){
                       snakeX[0]=25;
                   }
                   if(snakeX[0]<25){
                       snakeX[0]=750;
                   }
                   if(snakeY[0]<100){
                       snakeY[0]=725;
                   }
                   if(snakeY[0]>725){
                     snakeY[0]=100;
                   }

                   if(snakeX[0]==foodX & snakeY[0]==foodY){
                       lengh++;
                       foodX=((int) (Math.random()*30)+1)*25;
                       foodY=(new Random().nextInt(26)+4)*25;
                       score +=10;
                   }
                   //死亡判定
                   for(int i=1;i<lengh;i++){
                       if(snakeX[0]==snakeX[i]&& snakeY[0]==snakeY[i]){
                            isDie =true;
                       }
                   }
                  repaint();
               }
           }
       });
       timer.start();

     }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(new Color(148, 133, 32, 50));
        Images.headerImg.paintIcon(this,g,10,10);
        g.setColor(new Color(174, 246, 171, 255));
        g.fillRect(10,70,770,685 );

        //画蛇头蛇身
        if("R".equals(direction)){
            Images.rightImg.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        if("L".equals(direction)){
            Images.leftImg.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        if("U".equals(direction)){
            Images.upImg.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        if("D".equals(direction)){
            Images.downImg.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
//        Images.bodyImg.paintIcon(this,g,snakeX[1],snakeY[1]);
//        Images.bodyImg.paintIcon(this,g,snakeX[2],snakeY[2]);
        for (int i = 1; i < lengh; i++) {
            Images.bodyImg.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        //如果暂停
        if (isStart==false){
            g.setColor(new Color(22, 77, 196, 255));
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("点击空格开始游戏",250,330);
        }
        Images.foodImg.paintIcon(this,g,foodX,foodY);

        g.setColor(new Color(236, 17, 17, 255));
        g.setFont(new Font("微软雅黑", Font.BOLD, 20));
        g.drawString("分数:"+score,22,111);

        if(isDie){
            g.setColor(new Color(236, 17, 17, 255));
            g.setFont(new Font("微软雅黑", Font.BOLD, 20));
            g.drawString("游戏结束，重新请按空格:"+score,250,333);
        }
    }



}
